﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Kravl
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            cbWebSite.SelectedIndex = 0;
            cbSearchMethod.SelectedIndex = 3;
            cbMarDepth.SelectedIndex = 0;
            cbMaxVisitedSites.SelectedIndex = 0;

        }

        public int getMaxvisited()
        {
            int maxvisit = int.Parse(cbMaxVisitedSites.Text);
            return maxvisit;
        }

        private void click()
        {
            List<Uri> queue = new Crawl().processQueue(ref cbMaxVisitedSites, ref cbWebSite);
        }


        // exit
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        // Start Kravl ved dbl-klik på links-listen
        private void lbLinksFound_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            cbWebSite.Text = lbLinksFound.SelectedItem.ToString();
            btnCrawlUrl_Click(this, new EventArgs());
        }

        // Start Kravl ved dbl-klik på links-træet
        private void treeView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            cbWebSite.Text = treeView1.SelectedNode.Text;
            btnCrawlUrl_Click(this, new EventArgs());
        }



        // Start Kravl
        private void btnCrawlUrl_Click(object sender, EventArgs e)
        {
            // Clear
            textBox1.Clear();
            lbLinksFound.Items.Clear();
            treeView1.Nodes.Clear();

            // Opret en Uri fra textboxen
            Uri website = null;
            try
            {
                website = new Uri(cbWebSite.Text.Trim());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Fejl i indtastet website");
                return;
            }

            // vis siden i webbrowser
            webBrowser1.Url = website;

            // vis http response-stream
            new HttpWebRequest().GetWebpageUrls1(website, ref textBox1);

            switch (cbSearchMethod.Text)
            {
                case "Metode1": Metode1(website);
                    break;

                case "Metode2": Metode2(website);
                    break;

                case "Metode3": Metode3(website);
                    break;

                case "Metode4": Metode4(website);
                    break;
            }

        }


        /// <summary> Metode1 </summary>
        private void Metode1(Uri website)
        {
             // vis http response-stream
            // kører altid
            //new HttpWebRequest().GetWebpageUrls1(website, ref textBox1);
        }        


        /// <summary> Metode2 </summary>
        private void Metode2(Uri website)
        {
            // en mere brugbar test
            List<Uri> linksFromPage = new HttpWebRequest().GetWebpageUrls2(website);
            //textBox2.Text += "Metode2" + Environment.NewLine;
            foreach (Uri u in linksFromPage)
            {
                lbLinksFound.Items.Add(u.AbsoluteUri);
            }
        }        


        /// <summary> Metode3 </summary>
        private void Metode3(Uri website)
        {
            List<Uri> linksFromPage = new HttpWebRequest().GetWebpageUrls3(website);
            //textBox2.Text += "Metode3" + Environment.NewLine;
            foreach (Uri u in linksFromPage)
            {
                lbLinksFound.Items.Add(u.AbsoluteUri);
            }
        }


        /// <summary> Metode4 DFS med Max dybde </summary>
        private void Metode4(Uri website)
        {
            treeView1.Nodes.Add(new TreeNode(website.AbsoluteUri));

            // TODO: Søger kun 2-levels!
            List<Uri> linksFromPage = new HttpWebRequest().GetWebpageUrls3(website);
            foreach (Uri u1 in linksFromPage)
            {
                lbLinksFound.Items.Add(u1.AbsoluteUri);

                TreeNode n1 = new TreeNode();
                n1.Text = u1.AbsoluteUri;

                List<Uri> linksFromSubPage = new HttpWebRequest().GetWebpageUrls3(u1);
                foreach (Uri u2 in linksFromSubPage)
                {

                    lbLinksFound.Items.Add(u2.AbsoluteUri);

                    TreeNode n2 = new TreeNode();
                    n2.Text = u2.AbsoluteUri;
                    n1.Nodes.Add(n2);
                }
                treeView1.Nodes[0].Nodes.Add(n1);
            }



        }




        /// <summary> Metode5 </summary>
        private void Metode5(Uri website)
        {
            List<Uri> queue = new Crawl().processQueue(ref cbMaxVisitedSites, ref cbWebSite);
            foreach (Uri u in queue)
            {
                lbLinksFound.Items.Add(u.AbsoluteUri);
            }
        }





        private void button1_Click(object sender, EventArgs e)
        {
            //TreeNode2 rootNode = new TreeNode2("http://public.noea.dk/fen/testsite", 3);
            //TreeNode tn = new TreeNode();
            ////tn = GetChildNodes(rootNode);

            //rootNode.Nodes.Add( tn );


            //treeView1.Nodes.Add(tn);
        }

        HttpWebRequest req = new HttpWebRequest();

        /// <summary> virker ikke - den kan ikke tælle!!! </summary>
        private TreeNode GetChildNodes(TreeNode parent)
        {
            Uri parentLink = new Uri(parent.Text);
            List<Uri> links = req.GetWebpageUrls3(parentLink);
            
            List<TreeNode> ltn = new List<TreeNode>();
            foreach (Uri u in links)
            {
                ltn.Add(new TreeNode(u.AbsoluteUri));
            }
            TreeNode rtn = parent;
            rtn.Nodes.AddRange(ltn.ToArray());
            rtn.Nodes.Remove(parent);

            //loop
            if (links.Count > 0)
            {
                for (int i = 0; i < links.Count; i++)
                {
                    try
                    {
                        string test = rtn.Nodes[i].Text;
                        rtn.Nodes[i] = GetChildNodes(rtn.Nodes[i]);
                    }
                    catch (Exception ex)
                    {
                        textBox1.Text += "FEJL:" + Environment.NewLine + ex.Message + Environment.NewLine + Environment.NewLine;
                    }
                }
            }
            return rtn;
        }

















    }
}
